#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>

int main()
{
    pid_t pid = fork();
    
    if(pid > 0) {
        printf("Parent process, pid = %d, ppid = %d\n", getpid(), getppid());

        // 回收子进程
        // pid_t wpid = wait(NULL);
        // printf("Died child pid = %d\n", wpid);
        
        int status;
        pid_t wpid = wait(&status);
        // 判断是否为正常退出
        if(WIFEXITED(status)) {
            printf("exit value: %d\n", WEXITSTATUS(status));
        }
        
        // 判断是否为某个信号杀死
        if(WIFSIGNALED(status)) {
            printf("exit by signal: %d\n", WTERMSIG(status));
        }

    } else if(pid == 0) {
        sleep(2);
        printf("Child process, pid = %d, ppid = %d\n", getpid(), getppid());
    }

    for(int i = 0; i < 5; i++) {
       printf("i = %d ", i);
    }
    printf("\n");

    // return 0;
    return 9;
}

